package com.aoxon.cargo.component;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.activity.SupFillInfo1Activity;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.loader.FileLoader;
import com.aoxon.cargo.state.DataState;
import com.aoxon.cargo.state.MyStateUtil;

/**
 * 显示供应商信息的组件
 * 
 * @author ZZJ
 * 
 */
public class SupplierInfoShow {

	private TextView tvStoreName;
	private TextView tvCompanyName;
	private TextView tvStoreAddress;
	private ImageView ivCertification;
	private ImageView ivLogoPicture;
	private ProgressBar progressBar;

	private FileLoader imageLoader = new FileLoader(200, 200, false);

	public SupplierInfoShow(Activity activity) {
		this.progressBar = (ProgressBar) activity
				.findViewById(R.id.pbSupplierLogoLoading);
		this.tvStoreName = (TextView) activity
				.findViewById(R.id.tvSupplierStoreName);
		this.tvCompanyName = (TextView) activity
				.findViewById(R.id.tvSupplierCompanyName);
		this.tvStoreAddress = (TextView) activity
				.findViewById(R.id.tvSupplierStoreAddress);
		this.ivCertification = (ImageView) activity
				.findViewById(R.id.ivSupplierCertification);
		this.ivLogoPicture = (ImageView) activity
				.findViewById(R.id.ivSupplierLogo);
	}

	public void show(Supplier supplier) {

		tvStoreName.setText(supplier.getStoreName().toString());
		tvCompanyName.setText(supplier.getSupCompanyFullName());
		tvStoreAddress.setText(supplier.getSupCity() + supplier.getsupMarket()
				+ supplier.getsupStall());
		if (DataState.SUCCESS == supplier.getCerState()) {
			ivCertification.setVisibility(View.VISIBLE);
		}
		try {

			if (supplier.getSupLogo() != null
					&& supplier.getSupLogo().get(0).getPictureState() == DataState.SUCCESS) {
				String path = supplier.getSupLogo().get(0).getPictureUrl()
						+ "s.png";
				imageLoader.displayImage(path, ivLogoPicture, progressBar,
						false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			progressBar.setVisibility(View.GONE);
		}

	}

	public void setLogoOnClick(final Activity activity) {
		ivLogoPicture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(activity, SupFillInfo1Activity.class);
				activity.startActivity(intent);
				// Intent album = new Intent(Intent.ACTION_GET_CONTENT);
				// album.setType("image/*");
				// activity.startActivityForResult(album,
				// MyStateUtil.ALBUM_CODE);
			}
		});
	}

	public void setLogo(Bitmap bitmap) {
		ivLogoPicture.setImageBitmap(bitmap);
	}

	public void showProgressBar() {
		progressBar.setVisibility(View.VISIBLE);
	}

	public void hideProgressBar() {
		progressBar.setVisibility(View.GONE);
	}

}
